/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.kkkd.com/
 * 
 * ========================================================
 */
package com.simon.mengkou.ui.adapter;

import android.content.Context;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.ouertech.android.agm.lib.base.utils.UtilList;
import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.Niudan;
import com.simon.mengkou.data.bean.base.NiudanSKU;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.utils.UtilOuer;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author : Zhenshui.Xia
 * @since : 2016/1/27.
 * desc    : 订单适配器
 */
public class NDOrderAdapter extends AbsAdapter<Niudan> {
    public NDOrderAdapter(Context context, List<Niudan> data) {
        super(context, data);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Holder holder;
        if (null == convertView) {
            convertView = mInflater.inflate(R.layout.layout_order_list_item, null);
            holder = new Holder();
            convertView.setTag(holder);
            ButterKnife.bind(holder, convertView);
        } else {
            holder = (Holder) convertView.getTag();
        }

        Niudan data = getItem(position);

        OuerApplication.mImageLoader.loadImage(holder.mSdvImage, data.getImage().getUrl());
        holder.mTvName.setText(data.getName());
        List<NiudanSKU> skus = data.getSku();
        int num = 0;
        if(UtilList.isNotEmpty(skus)) {
            for(NiudanSKU sku : skus) {
                if(sku.isCheck()) {
                    num++;
                }
            }
        }

        holder.mTvNum.setText(mContext.getString(R.string.order_detail_num, num));
        String price = UtilOuer.formatPrice(data.getPrice());
        holder.mTvPrice.setText(Html.fromHtml(mContext.getString(R.string.order_detail_price, price)));
        String total = UtilOuer.formatPrice(data.getPrice() * num);
        holder.mTvTotal.setText(Html.fromHtml(mContext.getString(R.string.order_detail_total, total)));

        return convertView;
    }

    /**
     * @author zhenshui.xia
     */
    class Holder {
        //图片
        @Bind(R.id.order_id_image)
        SimpleDraweeView mSdvImage;
        //名称
        @Bind(R.id.order_id_name) TextView mTvName;
        //数量
        @Bind(R.id.order_id_num) TextView mTvNum;
        //价格
        @Bind(R.id.order_id_price) TextView mTvPrice;
        //总价
        @Bind(R.id.order_id_total) TextView mTvTotal;
    }
}
